package com.zwl.test;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class DialogActivity2 extends AppCompatActivity {

    private Context ctx;

    Handler h = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){


                case  1000:
        showPopwindow();

                    break;
            }
        }
    };
    private TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = this;



        initView();

    }

    private void initView() {


        t = (TextView) findViewById(R.id.text);

        t.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showPopwindow();
            }
        });
    }

int item = 0;
    private void showPopwindow() {

        View parent = ((TextView) findViewById(R.id.text));
//        View parent = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        View popView = View.inflate(ctx, R.layout.pop_menu2, null);

        ListView mListView = (ListView)popView.findViewById(R.id.mListView);
        mListView.setAdapter(new MyAdapter(ctx,infos,item));
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(popView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom1);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(ctx,"click---"+i,Toast.LENGTH_SHORT).show();
                item = i;


            }
        });



        ColorDrawable dw = new ColorDrawable(0x30000000);
        popWindow.setBackgroundDrawable(dw);
//        popWindow.showAtLocation(parent, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
        popWindow.showAsDropDown(parent,0,0);
    }

       String[] infos = {"全部","待选定","待签约","定向签约","进行中","已汇款","审核中","未通过","已取消"};

    private class MyAdapter extends BaseAdapter{

        Context context;
        String[] info;
        int item  = 0;
        public MyAdapter(Context context,String[] info,int item) {
            this.context = context;
            this.info = info;
            this.item = item;
        }

        @Override
        public int getCount() {
            return info.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if (view == null){
                view = View.inflate(ctx,R.layout.list_item,null);
            }

            TextView data = (TextView) view.findViewById(R.id.text_data);
            data.setText(info[i]);
item = 0;
            if (item == i) {
                System.out.println(item);

                data.setBackgroundResource(R.drawable.shape_recommend_blue_button);
                data.setTextColor(Color.WHITE);
            } else {
                data.setBackgroundResource(R.drawable.shape_recommend_white_button);
            }



            return view;
        }
    }
}
